#include "formt.h"
#include "ui_formt.h"
#include"teacherinfo.h"
#include "execsql.h"
#include <QMessageBox>
#include <QString>
#include<QSqlQuery>
#include <QSqlRecord>
#include<QDebug>
#include"teacher.h"

FormT::FormT(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::FormT)
{
    ui->setupUi(this);
}

FormT::~FormT()
{
    delete ui;
}

void FormT::showformt()
  {
    emit signalformt();
    this->show();
  }

void FormT::on_pb_ok_clicked()
{
   id =ui->lineEditT_id->text();
   ExecSQL exe;
   tinfo = exe.selectTeacherInfo(id);
    if(id == tinfo.getID() )
    {
        if(ui->lineEditT_name->text() == tinfo.getName())
        {
            if(ui->lineEditT_pswd->text()== ui->lineEditT_again->text())
            {
                QSqlQuery query;
                QString queryString =
                        QString("update TeacherInfoTable set loginpw = "+ui->lineEditT_again->text() +
                                " where ID  = '%1'").arg(id);
                if(query.exec(queryString))
                {
                    int id_id = query.record().indexOf("ID");
                    int TeacherId_id = query.record().indexOf("TeacherID");
                    int XID_id = query.record().indexOf("XID");
                    int Name_id = query.record().indexOf("Name");
                    int Sex_id = query.record().indexOf("Sex");
                    int Birth_id = query.record().indexOf("Birth");
                    int pswd_id = query.record().indexOf("loginpw");
                    while(query.next())
                    {
                        QString id = query.value(id_id).toString();
                        QString Name = query.value(Name_id).toString();
                        QString XID = query.value(XID_id).toString();
                        QString pswd = query.value(pswd_id).toString();
                        QString TeacherID = query.value(TeacherId_id).toString();
                        QString Sex = query.value(Sex_id).toString();
                        QString Birth = query.value(Birth_id).toString();
                         tinfo.updateTeacherInfo(id, TeacherID,XID, Name,Sex, Birth, pswd);
                         tinfo.display();
                      }
            }else{
                      qDebug()<<"---------999---------";
            }
            }
        }
    }else{
        QMessageBox msgBox;
        msgBox.setText("Error: Your ID is nonexistent !!");
        msgBox.setInformativeText("Please try again !");
        msgBox.setStandardButtons(QMessageBox::Retry | QMessageBox::Cancel);
        msgBox.setDefaultButton(QMessageBox::Retry);

        int ret = msgBox.exec();
        switch (ret) {
          case QMessageBox::Retry:
              ui->lineEditT_id->setFocus();
              break;
          case QMessageBox::Cancel:
              this->close();
              break;
          default:
              // should never be reached
              break;
        }
    }

}
void FormT::showTW()
{
    Teacher *tw = new Teacher;
    tw->show();
    tw->selectBoxSlot(str);
}
void FormT::on_pb_cancel_clicked()
{
    this->close();
   this->showTW();
}
